package com.lucien.system.service.impl;

import com.lucien.common.dto.UserList;
import com.lucien.common.fornt.SysRelationFront;
import com.lucien.common.utils.StringUtils;
import com.lucien.system.domain.SysRelationShip;
import com.lucien.system.domain.SysRelationShipExample;
import com.lucien.system.domain.SysUser;
import com.lucien.system.mapper.SysRelationShipMapper;
import com.lucien.system.service.ChatMessageService;
import com.lucien.system.service.SysRelationShipService;
import com.lucien.system.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Lucien
 * @Date 2021/5/23 14:03
 * @Version 1.0
 * @Description:
 */
@Service
public class SysRelationShipServiceImpl implements SysRelationShipService {

    @Autowired
    private SysRelationShipMapper relationShipMapper;

    @Autowired
    private SysUserService userService;

    @Autowired
    private ChatMessageService messageService;

    /**
     * 添加好友
     * @param username
     * @return
     */
    @Override
    public int appendFriend(String username) {
        SysUser sysUser = userService.selectByUsername(username);
        // 用户 -> 好友
        SysRelationShip userShip = new SysRelationShip();
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        userShip.setUserId(loginUser.getId());
        userShip.setFriendId(sysUser.getId());

        // 好友 -> 用户
        SysRelationShip friendShip = new SysRelationShip();
        friendShip.setUserId(sysUser.getId());
        friendShip.setFriendId(loginUser.getId());

        if (relationShipMapper.insert(userShip) != 1 || relationShipMapper.insert(friendShip) != 1 || StringUtils.isNull(sysUser)){
            return -1;
        }

        return 1;
    }

    /**
     * 获取用户好友列表
     * @return
     */
    @Override
    public List<UserList> userList() {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysRelationShipExample example = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(loginUser.getId());

        List<SysRelationShip> sysRelationShips = relationShipMapper.selectByExample(example);

        List<UserList> userList = new ArrayList<>();
        for (SysRelationShip sysRelationShip : sysRelationShips) {
            SysUser sysUser = userService.selectByUserId(sysRelationShip.getFriendId());
            UserList user = new UserList();
            user.setUserId(sysUser.getId());
            user.setUsername(sysUser.getUsername());
            user.setUserProfile(sysUser.getUserProfile());
            user.setGender(String.valueOf(sysUser.getGender()));
            user.setEmaill(sysUser.getEmail());
            user.setUserStateId(sysUser.getUserStateId());
            userList.add(user);
        }

        return userList;
    }

    /**
     * 删除好友
     * @param userId
     * @return
     */
    @Override
    public int delFriend(Integer userId) {
        //获取登录用户
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysRelationShipExample example = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(loginUser.getId());
        criteria.andFriendIdEqualTo(userId);
        //登录用户的好友列表
        List<SysRelationShip> userRelationShips = relationShipMapper.selectByExample(example);

        SysRelationShipExample example1 = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUserIdEqualTo(userId);
        criteria1.andFriendIdEqualTo(loginUser.getId());

        List<SysRelationShip> userRelationShips1 = relationShipMapper.selectByExample(example1);

        userRelationShips.addAll(userRelationShips1);

        for (SysRelationShip relationShip : userRelationShips) {
            relationShipMapper.deleteByPrimaryKey(relationShip.getId());
        }

        messageService.delMsg(userId);

        return 1;
    }

    /**
     * 判断是否为好友
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public SysRelationFront isFriends(Integer userId, Integer friendId) {
        SysRelationShipExample example = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andFriendIdEqualTo(friendId);

        List<SysRelationShip> ships = relationShipMapper.selectByExample(example);

        SysUser sysUser = userService.selectByUserId(friendId);
        SysRelationFront relationFront = new SysRelationFront();
        relationFront.setUserId(sysUser.getId());
        relationFront.setUsername(sysUser.getUsername());
        relationFront.setEmail(sysUser.getEmail());
        relationFront.setUserProfile(sysUser.getUserProfile());
        relationFront.setUserStateId(sysUser.getUserStateId());
        relationFront.setGender(sysUser.getGender());

        if(userId.equals(friendId)){
            relationFront.setFriendStatus(2);
        }
        else if (ships.size() == 0){
            relationFront.setFriendStatus(-1);
        }
        else {
            relationFront.setFriendStatus(1);
        }


        return relationFront;
    }

    @Override
    public boolean isFriend(Integer userId, Integer friendId) {
        SysRelationShipExample example = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId)
                .andFriendIdEqualTo(friendId);
        List<SysRelationShip> relationShips = relationShipMapper.selectByExample(example);

        SysRelationShipExample example1 = new SysRelationShipExample();
        SysRelationShipExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andFriendIdEqualTo(userId)
                .andUserIdEqualTo(friendId);
        List<SysRelationShip> relationShips1 = relationShipMapper.selectByExample(example1);

        relationShips.addAll(relationShips1);

        if (relationShips.size() == 0){
            return false;
        }
        return true;
    }

    /**
     * 获取用户好友ID列表
     * @param userId
     * @return
     */
    @Override
    public List<Integer> userFriendsList(Integer userId) {
        List<Integer> friends = new ArrayList<>();
        SysRelationShipExample example = new SysRelationShipExample();
        example.setOrderByClause("friend_id asc");
        SysRelationShipExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);

        List<SysRelationShip> relationShipList = relationShipMapper.selectByExample(example);

        for (SysRelationShip sysRelationShip : relationShipList) {
            friends.add(sysRelationShip.getFriendId());
        }

        return friends;
    }
}
